package cn.iocoder.yudao.module.paymember.controller.app.user;

import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserInfoRespVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.pay.service.packageorder.PackageOrderService;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP（有关笔试） - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppPayMemberUserController {

    @Resource
    private MemberUserService memberUserService;
    @Resource
    private MemberLevelService levelService;
    @Resource
    private PackageOrderService packageOrderService;
    @Resource
    private SocialUserService socialUserService;

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo(@RequestParam(value = "uid", required = false) Long uid) {
        Long loginUserId = Objects.nonNull(uid) ? uid : WebFrameworkUtils.getLoginUserId();
        MemberUserDO memberUserDO = memberUserService.getUser(loginUserId);
        MemberLevelDO level = levelService.getLevel(memberUserDO.getLevelId());
        AppMemberUserInfoRespVO appMemberUserInfoRespVO = MemberUserConvert.INSTANCE.convert(memberUserDO, level);
        appMemberUserInfoRespVO.setShare(memberUserService.getInviteLink(memberUserDO.getInviteCode()));
        appMemberUserInfoRespVO.setIfVip(packageOrderService.totalPrice(loginUserId) > 0);
        SocialUserRespDTO socialUserRespDTO = socialUserService.getSocialUserByUserId(UserTypeEnum.MEMBER.getValue(), loginUserId, SocialTypeEnum.WECHAT_OPEN.getType());
        appMemberUserInfoRespVO.setIfBindWeChat(Objects.nonNull(socialUserRespDTO));
        appMemberUserInfoRespVO.setIfSetPasswd(StringUtils.hasLength(memberUserDO.getPassword()));
        return success(appMemberUserInfoRespVO);
    }

}